package cn.iocoder.yudao.module.pay.splitting.mq;

import cn.hutool.extra.spring.SpringUtil;
import cn.iocoder.yudao.framework.common.exception.Assertions;
import cn.iocoder.yudao.module.api.member.agency.message.AgencyAuditPassMessage;
import cn.iocoder.yudao.module.api.member.agency.vo.AgencyApplyVO;
import cn.iocoder.yudao.module.api.pay.splitting.message.SplittingRequestMessage;
import cn.iocoder.yudao.module.api.shop.info.dto.ShopInfoDTO;
import cn.iocoder.yudao.module.api.shop.info.message.ShopAuditPassMessage;
import cn.iocoder.yudao.module.api.shop.revenue.message.RevenueCancelMessage;
import cn.iocoder.yudao.module.api.shop.revenue.message.RevenueUnfreezeMessage;
import cn.iocoder.yudao.module.pay.splitting.service.core.CorePaySplittingService;
import cn.iocoder.yudao.module.pay.splitting.service.core.CoreSplittingMerchantService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

/**
 * 分账商户 事件消费者
 *
 * @author 山野羡民
 */
@Component
@Slf4j
public class SplittingConsumer {

    @EventListener
    @Async // Spring Event 默认在 Producer 发送的线程，通过 @Async 实现异步
    public void onMessage(AgencyAuditPassMessage message) {
        AgencyApplyVO applyInfo = message.getApplyInfo();
        Assertions.notNull(applyInfo, "招商人员审核通过事件携带的申请信息为空");
        log.info("[pay/splitting][onMessage][招商人员审核通过事件(ID#{})]", applyInfo.getId());
        SpringUtil.getBean(CoreSplittingMerchantService.class).syncInfoIfNecessary(applyInfo.getContactMobile(), applyInfo.getContactName(), applyInfo.getOwnerEidNo());
    }

    @EventListener
    @Async // Spring Event 默认在 Producer 发送的线程，通过 @Async 实现异步
    public void onMessage(ShopAuditPassMessage message) {
        ShopInfoDTO shopInfo = message.getShopInfo();
        Assertions.notNull(shopInfo, "店铺审核通过事件携带的店铺信息为空");
        log.info("[pay/splitting][onMessage][店铺审核通过事件(ID#{})]", shopInfo.getId());
        SpringUtil.getBean(CoreSplittingMerchantService.class).syncInfoIfNecessary(shopInfo.getContactMobile(), shopInfo.getContactName(), shopInfo.getOwnerEidNo());
    }

    @EventListener
    @Async // Spring Event 默认在 Producer 发送的线程，通过 @Async 实现异步
    public void onMessage(RevenueUnfreezeMessage message) {
        log.info("[pay/splitting][onMessage][商家营收结算事件({})]", message);
    }

    @EventListener
    @Async // Spring Event 默认在 Producer 发送的线程，通过 @Async 实现异步
    public void onMessage(RevenueCancelMessage message) {
        log.info("[pay/splitting][onMessage][商家营收取消事件({})]", message);
    }

    @EventListener
    @Async // Spring Event 默认在 Producer 发送的线程，通过 @Async 实现异步
    public void onMessage(SplittingRequestMessage message) {
        log.info("[pay/splitting][onMessage][分账请求事件({})]", message);
        SpringUtil.getBean(CorePaySplittingService.class).doDepositAsyncSplitting(message.getPayNo(), message.getAmount());
    }

}
